package io.renren.modules.biz.tongtong;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.renren.common.redis.RedisUtils;
import io.renren.common.utils.MD5Utils;
import io.renren.modules.biz.tongtong.entity.LockEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ch --x
 * @version 1.0
 * Create by 2024/1/5 17:01
 * @ClassName TtlockHttp
 * @Description 请求通通锁接口
 */
@Component
public class TtlockHttp {

    @Resource
    private RedisUtils redisUtils;

    @Value("${tongtong.client-id}")
    private String clientId;

    @Value("${tongtong.client-secret}")
    private String clientSecret;

    @Value("${tongtong.username}")
    private String username;

    @Value("${tongtong.password}")
    private String password;

    @Value("${tongtong.token-url}")
    private String tokenUrl;

    @Value("${tongtong.ttlock-url}")
    private String ttlockUrl;


    /**
     * 获取请求token
     * @return
     */
    public String getToken(){

        String token = null;
        if (StringUtils.isEmpty(redisUtils.get("ttToken"))) {
            Map<String,Object> paramMap = new HashMap<String,Object>();
            paramMap.put("clientId",clientId);
            paramMap.put("clientSecret",clientSecret);
            paramMap.put("username",username);
            paramMap.put("password", MD5Utils.encrypt32(password));

            // 请求接口
            String res = HttpUtil.post(tokenUrl,paramMap);

            if (!StringUtils.isEmpty(res)) {
                JSONObject jsonObject = JSONUtil.parseObj(res);
                token = jsonObject.get("access_token").toString();
                // 保存到redis
                redisUtils.set("ttToken",token,Long.parseLong(jsonObject.get("expires_in").toString()));
                jsonObject.get("expires_in");
            }
        }else {
            token = redisUtils.get("ttToken").toString();
        }
        return token;
    }
    /**
     * 获取锁列表
     * @return
     */
    public Map<String,Object> getLockList(int pageNo, int pageSize){

        Map<String,Object> resultMap = new HashMap<>();

        List<LockEntity> resultList = new ArrayList<>();
        // 构造请求参数
        Map<String,Object> paramMap = new HashMap<String,Object>();
        paramMap.put("clientId",clientId);
        paramMap.put("accessToken",getToken());
        paramMap.put("pageNo",pageNo);
        paramMap.put("pageSize",pageSize);
        paramMap.put("date", System.currentTimeMillis());

        // 请求接口
        String res = HttpUtil.post(ttlockUrl,paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(res);
        if (jsonObject.get("list") != null) {
            resultList = JSONUtil.toList(jsonObject.get("list").toString(),LockEntity.class);
            resultMap.put("total",jsonObject.get("total"));
        }
        resultMap.put("list",resultList);
        return resultMap;

    }


}
